package com.goods.controller.business;


import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.OutStock;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.OutStockDetailVO;
import com.goods.common.vo.business.OutStockItemVO;
import com.goods.common.vo.business.OutStockVO;
import com.goods.service.OutStockService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/business/outStock")
public class OutStockController {

    @Autowired
    OutStockService outStockService;

    @GetMapping("/findOutStockList")
    public ResponseBean findOutStockList(Integer pageNum, Integer pageSize, Integer status,String outNum,String type){
        Map map = new HashMap();
        List<OutStock> outStockList = null;
        if (StringUtils.isEmpty(outNum) && StringUtils.isEmpty(type)){
            outStockList = outStockService.findOutStockList(status);
        }else {
            outStockList = outStockService.findOutStockList(status,outNum,type);
        }

        List<OutStockVO> collect = outStockList.stream().map(outStock -> {
            OutStockVO outStockVO = new OutStockVO();
            BeanUtils.copyProperties(outStock, outStockVO);
            Consumer consumer = outStockService.findConsumer(outStockVO.getConsumerId());
            outStockVO.setName(consumer.getName());
            outStockVO.setPhone(consumer.getPhone());
            return outStockVO;
        }).collect(Collectors.toList());

        List<OutStockVO> page = ListPageUtils.page(collect, pageSize, pageNum);
        map.put("total",outStockList.size());
        map.put("rows",page);
        return ResponseBean.success(map);
    }

    @GetMapping("/detail/{id}")
    public ResponseBean detail(@PathVariable Long id,Integer pageNum){
        List<OutStockItemVO> outStockItemVOList = outStockService.detail(id,pageNum);
        OutStockDetailVO outStockDetailVO = new OutStockDetailVO();
        outStockDetailVO.setItemVOS(outStockItemVOList);
        Consumer consumer = outStockService.findConsumerId(id);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer,consumerVO);
        outStockDetailVO.setConsumerVO(consumerVO);
        return ResponseBean.success(outStockDetailVO);
    }

    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id){
        int i = outStockService.remove(id);
        if (i == 0){
            return ResponseBean.error("移入回收站失败");
        }else {
            return ResponseBean.success();
        }
    }

    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id){
        int i = outStockService.back(id);
        if (i == 0){
            return ResponseBean.error("回收站恢复失败");
        }else {
            return ResponseBean.success();
        }
    }

    @GetMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        int i = outStockService.delete(id);
        if (i == 0){
            return ResponseBean.error("回收站删除失败");
        }else {
            return ResponseBean.success();
        }
    }

    @PostMapping("/addOutStock")
    public ResponseBean addOutStock(@RequestBody OutStockVO outStockVO){
        int i = outStockService.addOutStock(outStockVO);
        if (i == 0){
            return ResponseBean.error("添加失败");
        }else {
            return ResponseBean.success();
        }
    }

    @PostMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable long id){
        int i = outStockService.publish(id);
        if (i == 0){
            return ResponseBean.error("审核失败");
        }else {
            return ResponseBean.success();
        }
    }
}
